package HDAPI;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * A class of loading HDAPI configuration settings. 
 */
public class Settings extends Properties{
	private final static String DEFAULT_API_KEY = "f60e0ff93080929b62b82da9ece64f85";
	private final static String DEFAULT_COMMON_URL = "http://api.handsetdetection.com"; 
	
	public final static String COMMON_USERNAME = "common.email";
	public final static String COMMON_SECRET = "common.secret";
	public final static String COMMON_URL = "common.url";
	public final static String COMMON_SITE_ID = "common.site_id";
	public final static String COMMON_API_KEY = "common.api_key";
	
	public final static String CONNECT_TIMEOUT = "connection.connect_timeout";
	public final static String READ_TIMEOUT = "connection.read_timeout";
	public final static String PROXY_ENABLED = "proxy.enabled";
	public final static String PROXY_ADDRESS = "proxy.address";
	public final static String PROXY_PORT = "proxy.port";
	public final static String PROXY_USERNAME = "proxy.username";
	public final static String PROXY_PASSWORD = "proxy.password";
	public final static String CONFIG_NAME = "hdapi_config.properties";
	private static final long serialVersionUID = 1L;
	private static Settings g_instance;

	/**
	 * Load setting from default classpath root.
	 * @return
	 * @throws IOException
	 */
	public synchronized static boolean init() throws IOException
	{
		if (g_instance == null) {
			try {
				URL url = ClassLoader.getSystemResource(CONFIG_NAME);
				g_instance = new Settings();
				g_instance.load(url.openStream());
			} catch (IOException ie) {
				throw ie;
			}
		}
		return true;
	}
	
	/**
	 * Load setting from a specific java.io.InputStream object.
	 * @param is InputStream object
	 * @return
	 * @throws IOException
	 */
	public synchronized static boolean init(InputStream is) throws IOException
	{
		if (g_instance == null) {
			try {
				g_instance = new Settings();
				g_instance.load(is);
			} catch (IOException ie) {
				g_instance = null;
				throw ie;
			}
		}
		return true;
	}
	
	/**
	 * Getter of use proxy setting: proxy.enabled .
	 * @return Return true if use_proxy is not 0. Return false if setting value is 0.
	 */
	public static boolean isUseProxy() {
		return Boolean.parseBoolean(g_instance.getProperty(PROXY_ENABLED, "false"));
	}
	
	/**
	 * Getter of proxy server address setting: proxy.address . 
	 * @return
	 */
	public static String getProxyServer() {
		return g_instance.getProperty(PROXY_ADDRESS, "");
	}
	
	/**
	 * Getter of proxy port setting : proxy.port . 
	 * @return
	 */
	public static int getProxyPort() {
		int port = 80;
		try {
			port = Integer.parseInt(g_instance.getProperty(PROXY_PORT, "80"));
		} catch (NumberFormatException nfe) {
			System.out.println(nfe.getMessage());
		}
		return port;
	}
	
	/**
	 * Getter of proxy username for basic authentication: proxy.username.
	 * @return
	 */
	public static String getProxyUsername() {
		return g_instance.getProperty(PROXY_USERNAME, "");
	}
	
	/**
	 * Getter of proxy password for basic authentication: proxy.password
	 * @return
	 */
	public static String getProxyPassword() {
		return g_instance.getProperty(PROXY_PASSWORD, "");
	}
	
	
	/**
	 * Getter of API username setting: common.username
	 * @return
	 */
	public static String getUsername() {
		return g_instance.getProperty(COMMON_USERNAME, "");
	}
	
	/**
	 * Getter of API secret setting: common.secret
	 * @return
	 */
	public static String getSecret() {
		return g_instance.getProperty(COMMON_SECRET, "");
	}
	
	/**
	 * Getter of API key setting: common.api_key
	 * @return
	 */
	public static String getApiKey() {
		String apiKey = g_instance.getProperty(COMMON_API_KEY, DEFAULT_API_KEY);
		if ("".equals(apiKey.trim())) {
			apiKey = DEFAULT_API_KEY;
		}
		return apiKey;
	}
	
	/**
	 * Getter of site id setting: common.site_id
	 * @return
	 */
	public static String getSiteId() {
		return g_instance.getProperty(COMMON_SITE_ID, "");
	}
	
	/**
	 * Getter of URL of API server setting: common.urls  . The value of this setting can be one URL or multiple URLs separated by comma.
	 * @return
	 */
	public static String getURL() {
		String url = g_instance.getProperty(COMMON_URL, DEFAULT_COMMON_URL);
		if ("".equals(url.trim())) {
			url = DEFAULT_COMMON_URL;
		}
		return url;
	}
	
	/**
	 * Getter of socket connect timeout setting: connection.connect_timeout
	 * @return
	 */
	public static int getConnectTimeout() {
		return Integer.parseInt(g_instance.getProperty(CONNECT_TIMEOUT, "5"));
	}
	
	/**
	 * Getter of socket read timeout setting: connection.read_timeout
	 * @return
	 */
	public static int getReadTimeout() {
		return Integer.parseInt(g_instance.getProperty(READ_TIMEOUT, "5"));
	}
	
	
}
